package com.cooper.util;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.paoding.rose.web.Invocation;

import com.cooper.constants.AppConstants;
import com.cooper.domain.Entity;
import com.cooper.domain.UserSession;
import com.cooper.entity.UserInfo;
import com.cooper.vo.BaseVo;

/**
 * 
 * @Description: <br>
 * @Date: Jan 18, 2014 11:16:47 PM<br>
 * @Copyright (c) 2014 MiXuan <br> * 
 * @since 1.0
 * @author coral
 */
public final class Controllers {
	
	public static void prepareEntityForDataSaving(Entity entity, UserInfo userInfo, boolean updated){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		entity.setModifier(userInfo.getUserName());
		entity.setGmtModified(now);
		if(!updated){
			entity.setCreator(userInfo.getUserName());
			entity.setGmtCreate(now);
		}
	}
	
	public static UserInfo getUserInfo(Invocation inv){
		return getUserInfo(inv.getRequest().getSession());
	}
	
	public static UserInfo getUserInfo(HttpSession httpSession){
		UserSession userSession = Controllers.getUserSession(httpSession);
		if(null == userSession){
			return null;
		}
		return userSession.getUserInfo();
	}
	
	public static Long getUserId(Invocation inv){
		UserInfo userInfo = getUserInfo(inv);
		if(null == userInfo){
			return null;
		}
		return userInfo.getId();
	}
	
	public static UserSession getUserSession(Invocation inv){
		return getUserSession(inv.getRequest());
	}
	
	public static UserSession getUserSession(HttpServletRequest request){
		return getUserSession(request.getSession());
	}
	
	public static UserSession getUserSession(HttpSession httpSession){
		return (UserSession)httpSession.getAttribute(AppConstants.USER_SESSION_NAME);
	}
	
	public static void setUserSession(Invocation inv, UserSession userSession){
		inv.getRequest().getSession().setAttribute(AppConstants.USER_SESSION_NAME, userSession);
	}
	
	public static String getClientIp(Invocation inv){
		return getIpAddr(inv.getRequest());
	}
	
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	public static String getBasePath(Invocation inv) {
		HttpServletRequest request = inv.getRequest();
		return getBasePath(request);
	}
	
	public static String getBasePath(HttpServletRequest request) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(request.getScheme()).append("://").append(request.getServerName())
			.append(":").append(request.getServerPort()).append(request.getContextPath()).append("/");
		return buffer.toString();
	}
	
	public static void prepareBaseParamForDataSaving(BaseVo vo,Invocation inv,boolean updated){
		UserInfo userInfo = getUserInfo(inv);
		Timestamp now = new Timestamp(System.currentTimeMillis());
		vo.setModifier(userInfo.getUserName());
		vo.setGmtModified(now);
		if(!updated){
			vo.setCreator(userInfo.getUserName());
			vo.setGmtCreate(now);
		}
	}
	
	public static Object getServletContext(Invocation inv, String name){
		return inv.getServletContext().getAttribute(name);
	}
}
